# -*- coding:utf-8 -*-

# @Time : 2023/5/26 8:56
# @Author : 快乐的小猴子
# @Version : 
# @Function :
import os
import requests
from QiongYouTieBa.conf import settings


class QiongYouJi():
    def get_url(self, num):
        url_new = settings.url.format(num)
        return url_new

    def sends(self, url):
        resp = requests.get(url, headers=settings.headers).json()
        # resp = requests.get(url, headers=setting.headers, proxies=SetProxies().get_proxies()).text
        return resp

    def get_page_cnts(self):
        """
        :param resp: 该请求页面响应数据
        :return: pages  一共多少页数据
        """
        url_new = self.get_url(1)
        resp = self.sends(url_new)
        total_cnt = resp['data']['data']['total']
        if int(total_cnt) % 10 == 0:
            pages = int(total_cnt) / 10
        else:
            pages = int(total_cnt) / 10 + 1
        return pages

    def get_page_datas(self, resp):
        """
        :param resp: 该请求页面响应数据
        :return: page_datas_list    该页面筛选后的数据信息
        """
        page_datas_list = []
        data_list = resp['data']['data']['list']
        for i in range(0, len(data_list)):
            one_datas_dics = {}
            tid = data_list[i]['tid']  # tid
            user_name = data_list[i]['user_name']  # 用户名
            t_url = 'https:' + data_list[i]['t_url']  # 游记详情url
            c_time = data_list[i]['c_time']  # 创作时间
            views = data_list[i]['views']  # 浏览量
            comments = data_list[i]['comments']  # 评论数
            likes = data_list[i]['likes']  # 点赞数
            r_time = data_list[i]['r_time']  # 最后回复时间
            t_title = data_list[i]['t_title']  # 游记标题
            content = data_list[i]['content']  # 游记内容
            one_datas_dics['tid'] = tid
            one_datas_dics['user_name'] = user_name
            one_datas_dics['t_title'] = t_title
            one_datas_dics['likes'] = likes
            one_datas_dics['comments'] = comments
            one_datas_dics['views'] = views
            one_datas_dics['c_time'] = c_time
            one_datas_dics['r_time'] = r_time
            one_datas_dics['t_url'] = t_url
            one_datas_dics['content'] = content
            page_datas_list.append(one_datas_dics)
        return page_datas_list

    def more_datas(self):
        all_datas_list = []
        pages = self.get_page_cnts()
        # for i in range(1, pages+1):
        for i in range(1, 3):
            url_new = self.get_url(i)
            resp = self.sends(url_new)
            page_datas_list = self.get_page_datas(resp)
            all_datas_list.append(page_datas_list)
        return all_datas_list

    def verf_dir(self):
        file_path = os.getcwd() + '\\file\\'
        if not os.path.exists(file_path):
            os.makedirs(file_path)
        return file_path

    def save_files(self):
        print('Datas saving ......')
        num = 1
        all_datas_list = self.more_datas()
        file_path = self.verf_dir()
        for i in range(0, len(all_datas_list)):
            for j in range(0, len(all_datas_list[i])):
                tid = all_datas_list[i][j]['tid']
                name = all_datas_list[i][j]['user_name']
                title = all_datas_list[i][j]['t_title']
                likes = all_datas_list[i][j]['likes']
                comments = all_datas_list[i][j]['comments']
                views = all_datas_list[i][j]['views']
                c_time = all_datas_list[i][j]['c_time']
                r_time = all_datas_list[i][j]['r_time']
                t_url = all_datas_list[i][j]['t_url']
                content = all_datas_list[i][j]['content']
                with open('{}{}.txt'.format(file_path, tid), 'w', encoding='utf-8') as fp:
                    fp.write('ID|用户名|游记标题|点赞量|评论数|浏览量|创建时间|最后回复时间|游记详情URL|游记内容\n')
                    fp.write(str(tid) + '|' + name + '|' + title + '|' + str(likes) + '|' + str(comments) + '|' + str(
                        views) + '|' + c_time + '|' + r_time + '|' + t_url + '|' + content + '\n')
                    print('ID|用户名|游记标题|点赞量|评论数|浏览量|创建时间|最后回复时间|游记详情URL|游记内容\n')
                    print(num, str(tid) + '|' + name + '|' + title + '|' + str(likes) + '|' + str(comments) + '|' + str(
                        views) + '|' + c_time + '|' + r_time + '|' + t_url + '|' + content + '\n')
                print('第 {} 条游记 {} 保存完成'.format(num, title))
                num += 1
        print('Total {} success done!!!'.format(num))


# qiongyouji = QiongYouJi()
# qiongyouji.save_files()
